// 支付的方式(1微信，2支付宝，支付方式的标签为class为pay-method的那些元素)
var state;
// 获取倒计时元素
const countdownElement = document.getElementById("remaining-time");
// 设置倒计时初始时间
let minutes = 30;
let seconds = 0;
// 每一秒更新倒计时
const countdownInterval = setInterval(updateCountdown, 1000);


// 默认的支付方式
function getState() {
  // 获取 class 名为 pay-method 的元素
  const payMethod = document.querySelector(".pay-method");

  // 如果这个元素的 class 包括icon1，打印 1
  if (payMethod.classList.contains("icon1")) {
    var wxCircle = document.getElementById("wxzf");
    var zfbCircle = document.getElementById("zfbzf");
    wxCircle.src = "/static/api/icon/选中选中.png";
    zfbCircle.src = "/static/api/icon/未选中圆圈.png";
    this.state = "1";
  } else if (payMethod.classList.contains("icon2")) {
    var wxCircle = document.getElementById("wxzf");
    var zfbCircle = document.getElementById("zfbzf");
    wxCircle.src = "/static/api/icon/未选中圆圈.png";
    zfbCircle.src = "/static/api/icon/选中选中.png";
    this.state = "2";
  }
}


// 点击选中更换支付方式的样式和state
function pay(value) {
  this.state = value;

  if (value == 1) {
    var wxCircle = document.getElementById("wxzf");
    var zfbCircle = document.getElementById("zfbzf");
    wxCircle.src = "/static/api/icon/选中选中.png";
    zfbCircle.src = "/static/api/icon/未选中圆圈.png";
  } else if (value == 2) {
    var wxCircle = document.getElementById("wxzf");
    var zfbCircle = document.getElementById("zfbzf");
    wxCircle.src = "/static/api/icon/未选中圆圈.png";
    zfbCircle.src = "/static/api/icon/选中选中.png";
  }
}

// 支付按钮
function submit() {
  alert("当前方式为" + this.state);
}

// 倒计时功能
// 更新倒计时显示
function updateCountdown() {
  // 格式化时间
  const formattedMinutes = minutes.toString().padStart(2, "0");
  const formattedSeconds = seconds.toString().padStart(2, "0");

  // 更新倒计时元素内容
  countdownElement.textContent = `${formattedMinutes}:${formattedSeconds}`;

  // 倒计时结束
  if (minutes === 0 && seconds === 0) {
    clearInterval(countdownInterval);
  } else {
    // 减少一秒
    if (seconds === 0) {
      minutes--;
      seconds = 59;
    } else {
      seconds--;
    }
  }
}

// 页面加载时开始倒计时
window.addEventListener("load", updateCountdown);
window.addEventListener("load", getState);
